package 结构型模式.组合;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassNameCourseComponent
 * @Description
 * @Author
 * @Date2021/12/17 15:59
 **/


public class CourseComponentTest {
    public static void main(String[] args) {

        System.out.println("===========组合模式==============");
        CourseComponent javaBase = new Course("Java入门教程", 8280);
        CourseComponent ssm = new Course("SSM", 5000);
        CourseComponent packageCourse = new CoursePackage("Java架构师教程", 2);
        CourseComponent design = new Course("Java设计模式", 1500);
        CourseComponent source = new Course("源码分析", 2000);
        CourseComponent softSkill = new Course("软技能", 3000);
        packageCourse.add(design);
        packageCourse.add(source);
        packageCourse.add(softSkill);
        CourseComponent catalog = new CoursePackage("教程目录", 1);
        catalog.add(javaBase);
        catalog.add(ssm);
        catalog.add(packageCourse);
        catalog.print();
    }

}

// 抽象类，顶层组件
abstract class CourseComponent {
    public void add(CourseComponent Component) {
        throw new UnsupportedOperationException("不支持添加操作");
    }

    public void remove(CourseComponent Component) {
        throw new UnsupportedOperationException("不支持删除操作");
    }

    public String getName(CourseComponent Component) {
        throw new UnsupportedOperationException("不支持获取名称操作");
    }

    public double getPrice(CourseComponent Component) {
        throw new UnsupportedOperationException("不支持获得价格操作");
    }

    public void print() {
        throw new UnsupportedOperationException("不支持打印操作");
    }
}

// 具体课程类
class Course extends CourseComponent {
    private String name;
    private double price;

    public Course(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String getName(CourseComponent Component) {
        return this.name;
    }

    @Override
    public double getPrice(CourseComponent Component) {
        return this.price;
    }

    @Override
    public void print() {
        System.out.println(name + "(￥" + price + "元)");
    }
}
// 课程具体类
class CoursePackage extends CourseComponent{
    private List<CourseComponent> itmes = new ArrayList<CourseComponent>();
    private String name;
    private Integer level;

    public CoursePackage(String name, Integer level) {
        this.name = name;
        this.level = level;
    }

    @Override
    public void add(CourseComponent Component) {
        itmes.add(Component);
    }

    @Override
    public void remove(CourseComponent Component) {
        itmes.remove(Component);
    }

    @Override
    public String getName(CourseComponent Component) {
        return this.name;
    }

    @Override
    public void print() {
        System.out.println(this.name);
        for (CourseComponent c : itmes) {
            if (this.level != null) {
                for (int i = 0; i < this.level; i++) {
                    System.out.print("   ");
                }
                for (int i = 0; i < this.level; i++) {
                    if (i == 0) {
                        System.out.print("+");
                    }
                    System.out.print("-");
                }
            }
            c.print();
        }
    }
}
